﻿Imports Microsoft.VisualBasic.ControlChars

Public Class frmTabOverviewDescription
    Public Shared cEAttrOver As New Collection   'the overview descriptions
    Public Shared sKdelim As String = CrLf
    Dim editNode As EditXML = New EditXML()

    Private Sub btnAddEOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEOver.Click
        Dim s1 As String = ""
        Dim oNewOver As New EAoverClass
        Dim frmNewOver As New frmEAover(oNewOver)
        frmNewOver.ShowDialog()
        If (frmNewOver.getUpdated() = True) Then 'add the new overview
            oNewOver = frmNewOver.getEAOver()
            s1 = oNewOver.Overview
            s1 = Replace(s1, sKdelim, "  ")
            s1 = Microsoft.VisualBasic.Left(s1, 80)
            cEAttrOver.Add(oNewOver)
            lboxOverView.Items.Add(s1)
        End If
    End Sub

    Private Sub btnEditEOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditEOver.Click
        Dim iCount As Integer = lboxOverView.Items.Count
        Dim iSelInd As Integer = lboxOverView.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No entity overview selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim s1 As String = ""
        Dim oNewOver As EAoverClass = CType(cEAttrOver(iSelInd + 1), EAoverClass) '1-based index
        Dim frmNewOver As New frmEAover(oNewOver)
        frmNewOver.ShowDialog()
        If (frmNewOver.getUpdated() = True) Then 'update the overview containers
            oNewOver = frmNewOver.getEAOver()
            s1 = oNewOver.Overview
            s1 = Replace(s1, sKdelim, "  ")
            s1 = Microsoft.VisualBasic.Left(s1, 80)
            cEAttrOver.Add(oNewOver, , iSelInd + 1) 'insert before old version in collection; 1-based index
            cEAttrOver.Remove(iSelInd + 2)          'remove old version from collection
            lboxOverView.Items.Insert(iSelInd, s1) 'update list box; 0-based index
            lboxOverView.Items.RemoveAt(iSelInd + 1)
        End If
        btnEditEOver.Enabled = False
        btnDelEOver.Enabled = False

    End Sub

    Private Sub btnDelEOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelEOver.Click
        Dim iSelInd As Integer = lboxOverView.SelectedIndex

        If (iSelInd = -1) Then
            MessageBox.Show("No entity overview selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        cEAttrOver.Remove(iSelInd + 1) 'update collection, 1-based index
        lboxOverView.Items.RemoveAt(iSelInd) 'update list box
        btnEditEOver.Enabled = False
        btnDelEOver.Enabled = False

    End Sub

    Private Sub lboxOverView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxOverView.SelectedIndexChanged
        If lboxOverView.SelectedItems.Count() > 0 Then
            btnEditEOver.Enabled = True
            btnDelEOver.Enabled = True
        Else
            btnEditEOver.Enabled = False
            btnDelEOver.Enabled = False
        End If
    End Sub

    Private Sub frmTabOverviewDescription_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        editNode.Remove("/metadata/eainfo", "overview")
        For i As Integer = cEAttrOver.Count() To 1 Step -1
            Dim item As EAoverClass = cEAttrOver(i)
            Dim newNode As String = ""
            newNode = newNode & "<eaover>" & item.Overview & "</eaover>"
            For j As Integer = 1 To item.DetailCite.Count()
                newNode = newNode & "<eadetcit>" & item.DetailCite(j - 1) & "</eadetcit>"
            Next
            editNode.Add("/metadata/eainfo", "/detailed", "overview", newNode)
        Next
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("OverviewDescription")
        helpForm.ShowDialog()
    End Sub
End Class